package com.yksj.healthtalk.ui.home;

import java.util.List;

import org.handmark.pulltorefresh.library.PullToRefreshBase;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import org.handmark.pulltorefresh.library.PullToRefreshExpandableListView;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;

import com.yksj.healthtalk.adapter.DoctorExpandableListAdapter;
import com.yksj.healthtalk.entity.MyDoctorListEntity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.JsonParseUtils;

/**
 * 我的壹健康  我的医生
 * 
 */
public class MyDoctorListFrament extends Fragment implements OnChildClickListener, OnRefreshListener<ExpandableListView> {
	private PullToRefreshExpandableListView mPullListView;
	private ExpandableListView mExpandableListView;
	private DoctorExpandableListAdapter mNineImageAdapter;
	private String jsonAll;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.mydoctor_fragment_layout, null);
		mPullListView = (PullToRefreshExpandableListView) v.findViewById(R.id.hall);
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		mExpandableListView = mPullListView.getRefreshableView();
		mExpandableListView.setGroupIndicator(null);
		mNineImageAdapter = new DoctorExpandableListAdapter(getActivity());
		mExpandableListView.setAdapter(mNineImageAdapter);
		mExpandableListView.expandGroup(0);
		
		if(!SmartFoxClient.getLoginUserInfo().isDoctor())
		mExpandableListView.expandGroup(1);
		
		mExpandableListView.setOnChildClickListener(this);
		mPullListView.setOnRefreshListener(this);
		super.onActivityCreated(savedInstanceState);
	}

	private void initData() {
		HttpRestClient.doHttpMyDoctorList(SmartFoxClient.getLoginUserId(), new HttpHander());
	}
	
	@Override
	public void onStart() {
		super.onStart();
		mPullListView.setRefreshing();
		initData();
	}

	/**
	 * http请求
	 * 
	 * @author Administrator
	 * 
	 */
	class HttpHander extends AsyncHttpResponseHandler {

		@Override
		public void onSuccess(String content) {
			if (JsonParseUtils.filterErrorMessage(content) == null) {
				List<MyDoctorListEntity> myDoctorListData = JsonParseUtils.getMyDoctorListData(content);
				//只有是患者的时候才又更多
				if(!SmartFoxClient.getLoginUserInfo().isDoctor() && myDoctorListData.size()>1){
					MyDoctorListEntity entity = new MyDoctorListEntity();
					entity.setId(1000);
					entity.setName("更多...");
					myDoctorListData.add(3, entity);
				}
				mNineImageAdapter.onBound(myDoctorListData);
				jsonAll=content;
			}
			super.onSuccess(content);
		}
		@Override
		public void onFinish() {
			mPullListView.onRefreshComplete();
			super.onFinish();
		}

	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		Intent intent;
		// 列表序号 0-已开始服务 1-已预约服务 2-已取消预约服务 3-服务历史 4-未支付 5-支付失败 6-可（待）付款 7-我关注的医生
		 if (groupPosition == 0 && childPosition == 0) {
			intent = new Intent(getActivity(), MyDoctorDetailListActivity.class);
			intent.putExtra("name", mNineImageAdapter.entities.get(0).getName());
			intent.putExtra("index",String.valueOf(mNineImageAdapter.entities.get(0).getId()) );
			startActivity(intent);
		}else if(groupPosition==1 &&childPosition == 2){//更多
			intent = new Intent(getActivity(), MyDoctorMoreActivity.class);
			intent.putExtra("json",jsonAll);
			startActivity(intent);
		}else {
			intent = new Intent(getActivity(), MyDoctorDetailListActivity.class);
			intent.putExtra("name", mNineImageAdapter.entities.get(childPosition+1).getName());
			intent.putExtra("index",String.valueOf(mNineImageAdapter.entities.get(childPosition+1).getId()) );
			startActivity(intent);
		}
		return false;
	}

	@Override
	public void onRefresh(PullToRefreshBase<ExpandableListView> refreshView) {
		initData();
	}

}